<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <link rel="stylesheet" href="/node_modules/bootstrap/dist/css/bootstrap.min.css">
</head>
<style>
  .main {
    border: 1px solid #ccc;
    border-radius: 5px;
    width: 400px;
    height: 350px;
    margin: 250px auto;
    padding: 0 30px 30px 30px;
  }
</style>
<body>
  <div class="main">
    <div class="header">
      <a href="/">
        <img src="/public/img/logo.png" alt="">
      </a>
      <h1>用户登录</h1>
    </div>
    <form id = "login_form">
      <div class="form-group">
        <label for="">邮箱</label>
        <input type="email" name="email" id="email" class="form-control" placeholder="Email" autofocus>
      </div>
      <div class="form-group">
        <label for="">密码</label>
        <a href="" class="pull-right">忘记密码？</a>
        <input type="password" class="form-control" id="password" name="password" placeholder="Password">
      </div>
      <div class="checkbox">
        <label for="">
          <input type="checkbox">记住我
        </label>
      </div>
      <button type="submit" class="btn btn-success btn-block" >登录</button>
    </form>
    <div class="message">
      <p>没有账号？<a href="/register">点击注册</a>.</p>
    </div>
  </div>
  <script src="/node_modules/jquery/dist/jquery.js"></script>
  <script>
    $('#login_form').on('submit', function(e){
      e.preventDefault();
      let formData = $(this).serialize();
      // console.log(formData);
      $.ajax({
        url: '/login',
        type: 'post',
        data: formData,
        dataType: 'json',
        success: function (data) {
          let err_code = data.err_code;
          if (err_code === 0) {
            // window.alert("注册成功");
            // 由于异步请求，服务端无法实现重定向，客户端来进行重定向
            window.location.href = '/';
          } else if (err_code === 1) {
            window.alert("邮箱或密码错误");
          } else if (err_code === 500) {
            window.alert("服务器忙，请稍后重试");
          }
        }
      })
    })
  </script>
</body>

</html>